<template>
  <div>
    <h1>品牌列表</h1>
    <div>
      <el-form :inline="true" :model="listFrom" class="demo-form-inline">
        <el-form-item label="品牌名称">
          <el-input
            v-model="listFrom.keyWord"
            placeholder="请输入品牌名称"
            clearable
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="list()">查询</el-button>
          <el-button type="danger" @click="onAddBrand()">添加</el-button>
        </el-form-item>
      </el-form>
    </div>
    <el-table :data="tableData" border style="width: 100%">
      <el-table-column prop="brandId" label="品牌id" />
      <el-table-column prop="brandName" label="品牌名称" />
      <el-table-column prop="brandUse" label="使用数量" />
      <el-table-column prop="brandTop" label="排序" />
      <el-table-column prop="brandStatus" label="状态">
        <template #default="scope">
          <el-switch
            v-model="scope.row.brandStatus"
            :active-action-icon="View"
            :inactive-action-icon="Hide"
            :inactive-value="0"
            :active-value="1"
            @change="updateStatus(scope.row)"
          />
        </template>
      </el-table-column>
      <el-table-column prop="createBy" label="创建人" />
      <el-table-column prop="createTime" label="创建时间" />
      <el-table-column prop="updateBy" label="更新人" />
      <el-table-column prop="updateTime" label="更新时间" />
      <el-table-column label="操作" />
    </el-table>
    <div class="demo-pagination-block">
      <div class="demonstration"></div>
      <el-pagination
        v-model:current-page="listFrom.pageNum"
        v-model:page-size="listFrom.pageSize"
        :page-sizes="[2, 4, 6, 8]"
        :background="true"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
    <div>
      <el-dialog v-model="dialogFormVisible" title="添加品牌页面" width="500">
        <el-form :model="form" :rules="rules" ref="ruleFormRef">
          <el-form-item
            label="品牌名称"
            :label-width="formLabelWidth"
            prop="brandName"
          >
            <el-input v-model="form.brandName" autocomplete="off" />
          </el-form-item>
          <el-form-item
            label="使用数量"
            :label-width="formLabelWidth"
            prop="brandUse"
          >
            <el-input-number
              v-model="form.brandUse"
              :min="1"
              :max="10"
              @change="handleChange"
            />
          </el-form-item>
          <el-form-item
            label="排序"
            :label-width="formLabelWidth"
            prop="brandTop"
          >
            <el-input-number
              v-model="form.brandTop"
              :min="1"
              :max="10"
              @change="handleChange"
            />
          </el-form-item>
        </el-form>
        <template #footer>
          <div class="dialog-footer">
            <el-button @click="dialogFormVisible = false">取消</el-button>
            <el-button type="primary" @click="submitForm(ruleFormRef)">
              添加
            </el-button>
          </div>
        </template>
      </el-dialog>
    </div>
  </div>
</template>
<script lang="ts" setup>
import { onMounted, reactive, ref } from "vue";
import { getBrandList, addBrand, updateBrand } from "../api/api";
import { ElMessage, type FormInstance, type FormRules } from "element-plus";
import { Hide, View } from "@element-plus/icons-vue";
import { da } from "element-plus/es/locales.mjs";
const tableData = ref([]);
const ruleFormRef = ref<FormInstance>();
const listFrom = ref({
  pageNum: 1,
  pageSize: 2,
  keyWord: "",
});
const form = ref({
  brandName: "",
  brandUse: null,
  brandTop: null,
});
const dialogFormVisible = ref(false);
const formLabelWidth = "140px";
const total = ref(0);

//列表
const list = () => {
  getBrandList(listFrom.value).then((res) => {
    if (res.data.code == 200) {
      tableData.value = res.data.data.records;
      total.value = res.data.data.total;
    }
  });
};
//分页
const handleSizeChange = (val: number) => {
  console.log(`${val} items per page`);
  listFrom.value.pageSize = val;
  list();
};
const handleCurrentChange = (val: number) => {
  console.log(`current page: ${val}`);
  listFrom.value.pageNum = val;
  list();
};
//数字输入款
const handleChange = (value: number | undefined) => {
  console.log(value);
};
//添加
const onAddBrand = () => {
  dialogFormVisible.value = true;
};
//校验
const rules = reactive<FormRules>({
  brandName: [{ required: true, message: "请输入品牌名称", trigger: "blur" }],
  brandUse: [{ required: true, message: "请输入使用数量", trigger: "blur" }],
  brandTop: [{ required: true, message: "请输入品牌排序", trigger: "blur" }],
});
const submitForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  await formEl.validate((valid, fields) => {
    if (valid) {
      console.log("submit!");
      addBrand(form.value).then((res) => {
        if (res.data.code == 200) {
          ElMessage.success("添加成功");
          dialogFormVisible.value = false;
          list();
        } else {
          ElMessage.error(res.data.msg);
        }
      });
    } else {
      console.log("error submit!", fields);
    }
  });
};
const updateStatus = (row: any) => {
  updateBrand(row).then((res) => {
    if (res.data.code == 200) {
      ElMessage.success("修改成功");
      list();
    } else {
      ElMessage.error(res.data.msg);
    }
  });
};
onMounted(() => {
  list();
});
</script>
